$(document).ready(function(){
	$('.eventTooltip').each(function(){
		var toolTipLocation = 'topMiddle';
		var targetLocation = 'bottomMiddle';

		if($(this).hasClass('firstweek') && $(this).hasClass('firstday')){
			toolTipLocation = "leftTop";
			targetLocation = "rightBottom";
		} else if($(this).hasClass('lastweek') && $(this).hasClass('firstday')){
			toolTipLocation = "leftBottom";
			targetLocation = "rightTop";
		} else if($(this).hasClass('lastweek') && $(this).hasClass('lastday')){
			toolTipLocation = "rightBottom";
			targetLocation = "leftTop";
		} else if($(this).hasClass('firstweek') && $(this).hasClass('lastday')){
			toolTipLocation = "rightTop";
			targetLocation = "leftBottom";
		}

		$(this).qtip({
			content:{text:$(this).next().html()},
			show: {
				when: {target:false}, 
				ready: false 
			},
			position: {
				corner: {
				tooltip: toolTipLocation, 
				target: targetLocation 
			}
			},
			hide: {
				when: {target:false}
			},
			style: {
				border: {
				width: 5,
				radius: 0
			},
			width: 350,
			padding: 10,
			textAlign: 'left',
			tip: true, 
			name: 'blue' 
			}
		});
	});
});

function initDatePanels(){
	$('#startDate').datepicker().datepicker('option', {
		showAnim : 'fadeIn'
		}).datepicker('option', 'dateFormat', 'd M yy')
		.datepicker('setDate', new Date());
		
	$('#endDate').datepicker().datepicker('option', {
		showAnim : 'fadeIn'
		}).datepicker('option', 'dateFormat', 'd M yy')
		.datepicker('setDate',new Date());
	
	$('#repetitionEnd').datepicker().datepicker('option', {
		showAnim : 'fadeIn'
		}).datepicker('option', 'dateFormat', 'd M yy')
		.datepicker('setDate',new Date());
}

function initTimePanels(){
	$('#startTime').timeEntry({ampmPrefix: ' '});
	$('#endTime').timeEntry({ampmPrefix: ' '});
}

function dateFormat(date){
	var m_names = new Array("Jan", "Feb", "Mar", 
			"Apr", "May", "Jun", "Jul", "Aug", "Sep", 
			"Oct", "Nov", "Dec");
	var curr_date = date.getDate();
	var curr_month = date.getMonth();
	var curr_year = date.getFullYear();
	return curr_date+" "+m_names[curr_month]+" "+curr_year;
}

function timeFormat(d){
	var a_p = "";
	var curr_hour = d.getHours();
	if (curr_hour < 12){
		a_p = "AM";
	}
	else{
		a_p = "PM";
	}
	
	if (curr_hour == 0){
		curr_hour = 12;
	}
	if (curr_hour > 12){
		curr_hour = curr_hour - 12;
	}
	var curr_min = d.getMinutes();
	if(curr_hour<10)
		curr_hour = "0"+curr_hour;
	if(curr_min<10)
		curr_min = "0"+curr_min;
	return curr_hour + ":" + curr_min + " " + a_p;
}


$(document).ready(function(){
    $('.editable').click(function(event){
    	event.stopImmediatePropagation();
        $(this).next().next().children().each(function(){
           var $fieldName = $(this).attr('class');
           var $fieldValue = $(this).text();
           if($fieldName == "schPPL"){
        	   $("input[name$='schPPL']").val($fieldValue);
           }else{
        	   $('#'+$fieldName).val($fieldValue);
           }
        });
        
        var $startString = $('#schStart').val();
        var $endString = $('#schEnd').val();
        var $repEndString = $("#repetitionEnd").val();
        
        var val1 = ($startString.split('--'))[0];
        var val2 = ($startString.split('--'))[1];

        $('#delete').show();
        $('body').data('dialogbox').dialog('open');
        Edushare.AutoComplete.update();
        $('#startDate').val(val1);
        $('#endDate').val(($endString.split('--'))[0]);
        $('#startTime').val(val2);
        $('#endTime').val(($endString.split('--'))[1]);
        $('#repetitionEnd').val($repEndString);
     });
    $(".eventItem").click(function(event){
    	event.stopImmediatePropagation();
    	$(this).find(".editable").click();
    });
});

$(document).ready(function() {	
	var $dialog = $('<div></div>').html($('#calendarDialog').html()).dialog({
   			autoOpen: false,
   			title: 'Schedule Manager',
   			width:520,	
           	modal:true,
	       	closeOnEscape:true,
	       	close:function(){
			
       		$("#schId").val('');
       		$("#schTitle").val('');
       		$("#schLocation").val('');
       		$("#frequency").val('');
       		$("#schDesc").val('');
       		$('#wicketFeedbackPanel').html('');
       		$("input[name$='schPPL']").val('');
       		},
       		open:function(){
       			initDatePanels();
       			initTimePanels();
        		if($('#frequency').val() == "0"){
           			$('#repetitiondate').css('visibility','hidden').css('display','none');
        		}else{
           			$('#repetitiondate').css('visibility','visible').css('display','inline');
        		}
       		}
	});

	
	$('body').data('dialogbox',$dialog);
	$('#calendarDialog').remove();
	$('#repetitiondate').hide();


	$('.newScheduleLink').click(function(event){
		event.stopImmediatePropagation();
		var currentDate = new Date();
		var dateText = $(this).next('.fullDate').text();
		$('#delete').hide();
   		$("#schId").val('');
   		$("#schTitle").val('');
   		$("#schLocation").val('');
   		$("#frequency").val('');
   		$("#schDesc").val('');
   		$("input[name$='schPPL']").val('');
   		$('#wicketFeedbackPanel').html('');
		var date = new Date();
		var temp = new Array();
		temp = dateText.split("/");
		
		date.setDate(temp[0]*1);
		date.setMonth(temp[1]*1 - 1);
		date.setYear(temp[2]*1);
				
		$("#schTitle").focus();
		
		Edushare.AutoComplete.update();
		$('body').data('dialogbox').dialog('open');
		$('#startDate').datepicker('setDate', date);
		$('#endDate').datepicker('setDate', date);
		$('#repetitionEnd').datepicker('setDate', date);
		if(date.getDate()==currentDate.getDate() && 
				date.getMonth() == currentDate.getMonth() && 
				date.getFullYear()==currentDate.getFullYear()){
			var d = new Date();
			d.setMinutes(d.getMinutes()+10, 0, 0);
			$("#startTime").val(timeFormat(d));
			d.setMinutes(d.getMinutes()+30,0,0);
			$("#endTime").val(timeFormat(d));
		}else{
			$("#startTime").val("08:00 AM");
			$("#endTime").val("08:30 AM");
		}
		
	});
	
	$('.calendarEventContainer').click(function(event){
		event.stopImmediatePropagation();
		$(this).find('.newScheduleLink').click();
	});
	
    $('#frequency').change(function(){
		if($('#frequency').val() == "0"){
   			$('#repetitiondate').css('visibility','hidden').css('display','none');
		}else{
   			$('#repetitiondate').css('visibility','visible').css('display','inline');
		}
	});

	$('#frequency').change();
		
    $('.dateTooltip').click(function(){
		$(this).children().each(function(){
   			var $fieldName = $(this).attr('class');
   			var $fieldValue = $(this).text();
   			$('#'+$fieldName).val($fieldValue);
		});
		$('body').data('dialogbox').dialog('open');
	});
          
	$('.newScheduleLink').hover(
		function(){
			$(this).addClass("newScheduleLinkHover");
		},
		function(){
			$(this).removeClass("newScheduleLinkHover");
		}
	);
	
	$('#dateDialog').submit(function(){
		$('#schStart').val($('#startDate').val()+" "+$('#startTime').val());
		$('#schEnd').val($('#endDate').val()+ " "+$('#endTime').val());
		return true;
	});
	
	$('#showInvitation').toggle(
		function(){
			$('#invitationArea').show();
		},
		function(){
			$('#invitationArea').hide();
		}
	);
	$('#invitationArea').hide();
});				

